﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

public class Solution200
{
    public int NumIslands(char[][] grid)
    {
        int numIslands = 0; 

        for (int i = 0; i < grid.Length; i++)
        {


for(int j = 0; j < grid[i].Length; j++)
            {

                char c = grid[i][j];
                if (c == '1')
                {
                    //把这个岛转换成1
                    Change(grid, i, j);

                    numIslands++;
                }



            }


        }

        return numIslands;


    }

    private void Change(char[][] grid, int row, int col)
    {
int m=grid.Length;
        int n = grid[0].Length;

        grid[row][col] = '0';

        if(row+1< m && grid[row + 1][col]=='1')
        {
            Change(grid, row + 1, col);
        }


        if (row - 1 >=0 && grid[row - 1][col] == '1')
        {
            Change(grid, row - 1, col);
        }


        if (col + 1 <n && grid[row ][col + 1] == '1')
        {
            Change(grid, row , col + 1);
        }


        if (col - 1 >= 0 && grid[row][col - 1] == '1')
        {
            Change(grid, row, col - 1);
        }


    }
}


